<!-- File: /app/views/users/messages.ctp -->
<script>
$(function() {
	$('#message_deleting').click(function(){

		var href = $(this).attr('href');
		
		$('#msg_deleting_dialog').dialog({
			title: href,
			buttons: [
			{
				text: 'Yes',
				click: function() {
					window.location: href;
				}
			},
			{
				text: 'No',
				click: function() { 
					$(this).dialog("close"); 
				}
			}
			]
			
		});
		return false;
	});
	
});
</script>

<div class="user form">
	
	<h2>Messages</h2>
	<table>
	<tr>
		<td class="actions" width="60">
		<?php 
			echo $html->link(__('Received', true), array('controller'=>'messages','action' => 'index'));
			if (!empty($message['Message']['id'])) 
				echo $html->link(__('Reply', true), array('controller'=>'messages', 'action' => 'index', $message['Message']['id'], 'reply'))
		?>
		</td>
 		<td class="actions">
 		<?php echo $this->Html->link(__('Compose New', true), array('action' => 'compose')); ?>
 		</td>
    </tr>
    </table>

	<table>
		<?php 
		/* if the 'view' button is not clicked */
		if (empty($message['Message']['id'])) {
			
			echo $this->Html->tableHeaders(array('From', 'Subject', 'Date Received', 'Action', ''));
			foreach ($messages as $msg): 
				if ($msg['Message']['receiver_id'] == $user['User']['id']) {
					$sender = $msg['Message']['sender_id'] == $user['User']['id']? 'me' : $msg['User']['first_name'] . ' ' . $msg['User']['last_name'];
					echo $html->tableCells(array(
						$sender,
						$msg['Message']['subject'],
						$msg['Message']['date_received'],
						$html->link('view', array('controller'=>'messages', 'action'=>'index' , $msg['Message']['id'])),
						$this->Html->link('delete', array('controller'=>'messages','action'=>'delete', $msg['Message']['id']), array('id'=>'message_deleting'))
					));
				
				}
			endforeach;
			
			 
		} else {
				
			if ($replyIsClicked) {
				echo $this->Form->create('Message', array('action'=>'compose','enctype' => 'multipart/form-data'));
	   			echo $form->input('emails', array('label'=>'Send To:', 'value'=>$message['User']['email_addr']));
    			echo $form->input('subject', array('value'=>$message['Message']['subject']));
    			echo $form->input('body', array('type'=>'textarea', 'value'=>$message['Message']['body']));
    			echo $form->end('Reply');
   
			} else {
				
				echo "From: " .  $message['User']['first_name'] . " " . $message['User']['last_name'] ."<p>";
				echo 'To: me<p>';
				echo "Subject: " . $message['Message']['subject'] . "<p>";
				echo "Date: " . $message['Message']['date_received'] . "<p>";
				echo "<hr><p>";
				echo $message['Message']['body'];
			}
		} 
		
		?>
	</table>

	<div id="msg_deleting_dialog" style="display:none;" >Delete this msg?</div>
</div>